//
//  ZYCNewsBulletinBottomView.m
//  ZDSC
//
//  Created by ZDTX on 2021/8/2.
//

#import "ZYCNewsBulletinBottomView.h"
#import "ZYCNewsBulletionAnnexCell.h"
#import "ZYCNewBulletinModel.h"
@interface ZYCNewsBulletinBottomView ()<UITableViewDataSource,UITableViewDelegate,ZYCNewsBulletionAnnexCellDelegate>

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UITableView *tableView;

@end
@implementation ZYCNewsBulletinBottomView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = HEX_RGB(@"#FFFFFF");
        [self addSubview:self.titleLabel];
        [self addSubview:self.tableView];
        
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(15.0f);
            make.top.equalTo(self).offset(20.0f);
        }];
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(15.0f);
            make.right.equalTo(self).offset(-15.0f);
            make.top.equalTo(self.titleLabel.mas_bottom);
            make.bottom.equalTo(self);
        }];
    }
    return self;
}

- (void)setFilesList:(NSArray *)filesList{
    _filesList = filesList;
    if (filesList.count > 0) {
        _titleLabel.hidden = NO;
    }else{
        _titleLabel.hidden = YES;
    }
    [self.tableView reloadData];
}


#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.filesList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYCNewsBulletionAnnexCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCNewsBulletionAnnexCell class]) forIndexPath:indexPath];
    cell.model = self.filesList[indexPath.row];
    cell.delegate = self;
    return cell;
}
-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45.0f;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}


#pragma mark - ZYCNewsBulletionAnnexCellDelegate
- (void)newsBulletionAnnexCellDidDownload:(ZYCNoticeFileModel *)model{
    if (self.downloadBlock) {
        self.downloadBlock(model);
    }
}
#pragma mark - getters
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.scrollEnabled = NO;
        [_tableView registerClass:[ZYCNewsBulletionAnnexCell class] forCellReuseIdentifier:NSStringFromClass([ZYCNewsBulletionAnnexCell class])];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = HEX_RGB(@"#FAFAFA");
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
    }
    return _tableView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel LabelWithString:@"附件" Font:kCustomFont(@"PingFangSC-Regular", 16.0f) titleColor:HEX_RGB(@"#333333") textAlignment:ZZTextAlignmentLeft];
    }
    return _titleLabel;
}


@end
